<?php

$installer = $this;

$installer->startSetup();

$installer->run("
SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS {$this->getTable('productlabels/productlabels')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/productlabels')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `actions` text CHARACTER SET utf8,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=39 ;

DROP TABLE IF EXISTS {$this->getTable('productlabels/attribute')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/attribute')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name_attribute` varchar(255) NOT NULL,
  `type` varchar(255) NOT NULL,
  `scope_global` smallint(1) NOT NULL DEFAULT '0',
  `title` varchar(255) NOT NULL,
  `require` tinyint(1) NOT NULL DEFAULT '0',
  `class` varchar(255) NOT NULL,
  `type_backend` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

DROP TABLE IF EXISTS {$this->getTable('productlabels/css')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/css')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content` text NOT NULL,
  `store` smallint(5) NOT NULL,
  `scope_global` smallint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `store_id` (`store`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

DROP TABLE IF EXISTS {$this->getTable('productlabels/type_varchar')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/type_varchar')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label_id` int(11) NOT NULL,
  `attribute_id` int(11) NOT NULL,
  `store_id` smallint(5) unsigned NOT NULL,
  `value` varchar(555) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `store_id` (`store_id`),
  KEY `label_id` (`label_id`),
  KEY `attribute_id` (`attribute_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32 ;

DROP TABLE IF EXISTS {$this->getTable('productlabels/type_text')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/type_text')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label_id` int(11) NOT NULL,
  `attribute_id` int(11) NOT NULL,
  `store_id` smallint(5) unsigned NOT NULL,
  `value` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `store_id` (`store_id`),
  KEY `label_id` (`label_id`,`attribute_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

DROP TABLE IF EXISTS {$this->getTable('productlabels/type_int')};
CREATE TABLE IF NOT EXISTS {$this->getTable('productlabels/type_int')} (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `label_id` int(11) NOT NULL,
  `attribute_id` int(11) NOT NULL,
  `store_id` smallint(5) unsigned NOT NULL,
  `value` tinyint(1) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `store_id` (`store_id`),
  KEY `label_id` (`label_id`),
  KEY `attribute_id` (`attribute_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=40 ;




ALTER TABLE {$this->getTable('productlabels/type_varchar')}
  ADD CONSTRAINT {$this->getTable('productlabels/type_varchar')} FOREIGN KEY (`store_id`) REFERENCES {$this->getTable('core_store')} (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `{$this->getTable('productlabels/type_varchar')}_ibfk_1` FOREIGN KEY (`label_id`) REFERENCES {$this->getTable('productlabels/productlabels')} (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE {$this->getTable('productlabels/type_text')}
  ADD CONSTRAINT {$this->getTable('productlabels/type_text')} FOREIGN KEY (`store_id`) REFERENCES {$this->getTable('core_store')} (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `{$this->getTable('productlabels/type_text')}_ibfk_1` FOREIGN KEY (`label_id`) REFERENCES {$this->getTable('productlabels/productlabels')} (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE {$this->getTable('productlabels/type_int')}
  ADD CONSTRAINT {$this->getTable('productlabels/type_int')} FOREIGN KEY (`store_id`) REFERENCES {$this->getTable('core_store')} (`store_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `{$this->getTable('productlabels/type_int')}_ibfk_1` FOREIGN KEY (`label_id`) REFERENCES {$this->getTable('productlabels/productlabels')} (`id`) ON DELETE CASCADE ON UPDATE CASCADE;



INSERT INTO {$this->getTable('productlabels/productlabels')} (`id`, `name`, `actions`) VALUES
(32, 'label1', 'a:7:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";s:10:\"conditions\";a:1:{i:0;a:5:{s:4:\"type\";s:32:\"salesrule/rule_condition_product\";s:9:\"attribute\";s:20:\"quote_item_row_total\";s:8:\"operator\";s:2:\"==\";s:5:\"value\";s:2:\"ok\";s:18:\"is_value_processed\";b:0;}}}'),
(33, 'label2', 'a:7:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";s:10:\"conditions\";a:1:{i:0;a:5:{s:4:\"type\";s:32:\"salesrule/rule_condition_product\";s:9:\"attribute\";s:20:\"quote_item_row_total\";s:8:\"operator\";s:2:\"==\";s:5:\"value\";s:2:\"ok\";s:18:\"is_value_processed\";b:0;}}}'),
(36, 'label3', 'a:7:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";s:10:\"conditions\";a:1:{i:0;a:5:{s:4:\"type\";s:32:\"salesrule/rule_condition_product\";s:9:\"attribute\";s:13:\"special_price\";s:8:\"operator\";s:2:\">=\";s:5:\"value\";d:70;s:18:\"is_value_processed\";b:0;}}}'),
(38, 'label4', 'a:7:{s:4:\"type\";s:40:\"salesrule/rule_condition_product_combine\";s:9:\"attribute\";N;s:8:\"operator\";N;s:5:\"value\";s:1:\"1\";s:18:\"is_value_processed\";N;s:10:\"aggregator\";s:3:\"all\";s:10:\"conditions\";a:1:{i:0;a:5:{s:4:\"type\";s:32:\"salesrule/rule_condition_product\";s:9:\"attribute\";s:12:\"category_ids\";s:8:\"operator\";s:2:\"{}\";s:5:\"value\";s:2:\"10\";s:18:\"is_value_processed\";b:0;}}}');

INSERT INTO {$this->getTable('productlabels/attribute')} (`id`, `name_attribute`, `type`, `scope_global`, `title`, `require`, `class`, `type_backend`) VALUES
(1, 'name', '', 1, 'Label Name', 1, 'required-entry', 'text'),
(2, 'image', 'varchar', 0, 'Image', 0, '', 'file'),
(3, 'background', 'varchar', 0, 'Background', 0, '', 'file'),
(4, 'texthtml', 'text', 0, 'Text', 0, '', 'editor'),
(5, 'css_class', 'varchar', 0, 'Css Class', 0, '', 'text'),
(6, 'enable', 'int', 2, 'Enable', 0, '', 'checkbox');

INSERT INTO {$this->getTable('productlabels/css')} (`id`, `content`, `store`, `scope_global`) VALUES
(2, '.english-label{color:white;width:74px;height:76px}\r\n.english-label p {padding:8px 20px 20px 20px;text-align:center;}\r\n.products-list li.item {position: relative;}\r\n.english-label {top: 80px;left: 10px; position: absolute;}\r\n.products-grid li.item {position: relative;}', 0, 0);

INSERT INTO {$this->getTable('productlabels/type_varchar')} (`id`, `label_id`, `attribute_id`, `store_id`, `value`) VALUES
(22, 32, 2, 0, 'store_0_images.jpeg'),
(23, 32, 3, 0, 'store_0_images.jpeg'),
(24, 32, 5, 0, 'label1_css'),
(25, 32, 2, 1, 'store_1_logo.png'),
(26, 36, 2, 0, 'store_0_article-demo1.jpg'),
(27, 36, 3, 0, 'nopic'),
(28, 36, 5, 1, 'english-label'),
(29, 36, 3, 1, 'store_1_saleoff_.png'),
(31, 36, 2, 1, 'nopic');

INSERT INTO {$this->getTable('productlabels/type_text')} (`id`, `label_id`, `attribute_id`, `store_id`, `value`) VALUES
(11, 32, 4, 0, '{{var save_percent}}{{var save_price}}{{var product.price}}'),
(12, 33, 4, 0, '{{var save_percent}}'),
(14, 32, 4, 1, '{{var save_percent}}{{var save_price}}{{var product.price}}{{var highttest_price}}'),
(15, 36, 4, 0, '{{var save_percent}} {{var product.price}} sdfsdfsd {{var product.special_price}} fsdfsdf'),
(16, 36, 4, 1, 'Sale off {{var save_percent}}'),
(17, 38, 4, 0, '{{var product.special_price}}');

INSERT INTO {$this->getTable('productlabels/type_int')} (`id`, `label_id`, `attribute_id`, `store_id`, `value`) VALUES
(17, 32, 6, 0, 1),
(18, 32, 6, 1, 1),
(19, 33, 6, 0, 2),
(28, 33, 6, 1, 1),
(29, 36, 6, 0, 2),
(30, 33, 6, 3, 1),
(31, 36, 6, 3, 1),
(35, 32, 6, 3, 2),
(37, 36, 6, 1, 1),
(39, 38, 6, 0, 1);
    ");

$installer->endSetup(); 